package com.arlin.springcloud.controller;

import com.arlin.springcloud.pojo.Dept;
import com.arlin.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: DeptController
 * @Description: TODO
 * @Author: arlin
 * @Date: 2021/3/21
 */
@RestController
public class DeptController {

    private final DeptService deptService;

    public DeptController(DeptService deptService) {
        this.deptService = deptService;
    }

    @GetMapping("/dept/get/{id}")
    @HystrixCommand(fallbackMethod = "hystrixGet")
    public Dept getDept(@PathVariable("id") Long id) {
        Dept dept = deptService.queryById(id);
        if(dept == null) {
            throw new RuntimeException("id==>"+ id + "用户不存在");
        }
        return dept;
    }

    //备选方法
    public Dept hystrixGet(@PathVariable("id") Long id){
        return new Dept()
                .setDeptNo(id)
                .setName("id==>"+ id + "没有对应的信息，null---hystrix")
                .setDbSource("no this database in MySQL");
    }
}
